import { Controller, Get, Post, Body, Patch, Param, Delete, HttpCode, Query, UseGuards, UploadedFile, UseInterceptors } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiResponse, ApiOperation, ApiBody, ApiBearerAuth, ApiTags, ApiConsumes } from '@nestjs/swagger';
import { IdDto } from 'src/common/dto/dto';
import { GetMovieDto, CreateMovieDto, UpdateMovieDto, RemoveRecommendationDto, AddRecommendationsDto } from './dto/movie.dto';
import { MovieEntity } from './entities/movie.entity';
import { MovieService } from './movies.service';
import { LogOperation } from '../operation-record/log.decorator';
import { OperateType, OperationRecordType } from '../operation-record/entities/operation-record.entity';
import { FileInterceptor } from '@nestjs/platform-express';

@ApiBearerAuth() // swagger文档设置token
@ApiTags('电影管理')
@Controller('admin/movie')
export class MovieController {
  constructor(private readonly movieService: MovieService) { }

  /** 获取电影列表 */
  @HttpCode(200)
  @Get('/getMoviePage')
  @ApiResponse({
    status: 200,
    description: '电影列表',
    type: MovieEntity,
    isArray: true,
  })
  @ApiOperation({
    tags: ['获取电影'],
    summary: '获取电影列表',
    operationId: 'getMoviePage',
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  async getMoviePage(@Query() params: GetMovieDto) {
    return this.movieService.getMoviePage(params);
  }

  /** 创建电影 */
  @HttpCode(200)
  @Post('/createMovie')
  @ApiBody({
    description: '创建电影',
    type: CreateMovieDto,
  })
  @ApiOperation({
    summary: '创建电影',
    operationId: 'createMovie',
  })
  @LogOperation({
    operateType: OperateType.add,
    operation: `创建电影`,
    name: '创建',
    outside: OperationRecordType.movies,
    entity: MovieEntity,
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  async createMovie(@Body() body: CreateMovieDto) {
    return this.movieService.createMovie(body);
  }

  /** 更新电影 */
  @HttpCode(200)
  @Post('/updateMovie')
  @ApiBody({
    description: '更新电影信息',
    type: UpdateMovieDto,
  })
  @ApiOperation({
    summary: '更新电影信息',
    operationId: 'updateMovie',
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  async updateMovie(@Body() body: UpdateMovieDto) {
    return this.movieService.updateMovie(body);
  }

  /** 删除电影 */
  @HttpCode(200)
  @Post('/deleteMovie')
  @ApiBody({
    description: '删除电影',
    type: IdDto,
  })
  @ApiOperation({
    summary: '删除电影',
    operationId: 'deleteMovie',
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  async deleteMovie(@Body() body: IdDto) {
    return this.movieService.deleteMovie(body);
  }

  /** 添加推荐剧集 */
  @HttpCode(200)
  @Post('/addRecommendations')
  @ApiBody({
    description: '推荐剧集ID列表',
    type: AddRecommendationsDto,
  })
  @ApiOperation({
    summary: '添加推荐剧集',
    operationId: 'addRecommendations',
  })
  @UseGuards(AuthGuard('jwt'))
  async addRecommendations(
    @Body() body: AddRecommendationsDto,
  ) {
    return this.movieService.addRecommendations(body.movieId, body.recommendedIds);
  }

  /** 获取推荐剧集 */
  @HttpCode(200)
  @Get('/getRecommendations/:id')
  @ApiOperation({
    summary: '获取推荐剧集',
    operationId: 'getRecommendations',
  })
  @UseGuards(AuthGuard('jwt'))
  async getRecommendations(@Param('id') movieId: number) {
    return this.movieService.getRecommendations(movieId);
  }
  /** 获取电影详情 */
  @HttpCode(200)
  @Get('/getMovieInfo/:id')
  @ApiOperation({
    summary: '获取推荐剧集',
    operationId: 'getMovieInfo',
  })
  @UseGuards(AuthGuard('jwt'))
  async getMovieInfo(@Param('id') movieId: number) {
    return this.movieService.getMovieInfo(movieId);
  }

  /** 删除推荐剧集 */
  @HttpCode(200)
  @Post('/removeRecommendation')
  @ApiBody({
    description: '推荐剧集ID',
    type: RemoveRecommendationDto,
  })
  @ApiOperation({
    summary: '删除推荐剧集',
    operationId: 'removeRecommendation',
  })
  @UseGuards(AuthGuard('jwt'))
  async removeRecommendation(
    @Body() body: RemoveRecommendationDto,
  ) {
    return this.movieService.removeRecommendation(body.movieId, body.recommendedId);
  }

  @Post('/importJson')
  @UseInterceptors(FileInterceptor('file'))
  @ApiOperation({ summary: 'json导入', operationId: 'upload' })
  @ApiConsumes('multipart/form-data')
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        file: {
          type: 'string',
          format: 'binary',
        },
      },
    },
  })
  async uploadFile(@UploadedFile() file: any) {
    const jsonData = JSON.parse(file.buffer.toString());
    return await this.movieService.importJson(jsonData)
  }
}
